Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Roc authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_core/target_stdio/roc_core/log.h
10//! @brief Logging.
11
12#ifndef ROC_CORE_LOG_H_
13#define ROC_CORE_LOG_H_
14
15#include "roc_core/attributes.h"
16#include "roc_core/mutex.h"
18#include "roc_core/singleton.h"
19
20#ifndef ROC_MODULE
21#error "ROC_MODULE not defined"
22#endif
23
24//! Print message to log.
25#define roc_log(...) \
26 ::roc::core::Logger::instance().print(ROC_STRINGIZE(ROC_MODULE), __VA_ARGS__)
27
28namespace roc {
29
30//! Log level.
32 LogNone, //!< Disable all messages.
33 LogError, //!< Error message.
34 LogInfo, //!< Informational message.
35 LogDebug, //!< Debug message.
36 LogTrace //!< Debug message (extra verbosity).
37};
38
39namespace core {
40
41//! Default log level.
43
44//! Colors mode.
46 ColorsDisabled, //!< Do not use colored logs.
47 ColorsEnabled //!< Use colored logs.
48};
49
50//! Default colors mode.
52
53//! Log handler.
54typedef void (*LogHandler)(LogLevel level, const char* module, const char* message);
55
56//! Logger.
57class Logger : public NonCopyable<> {
58public:
59 //! Get logger instance.
60 static Logger& instance() {
62 }
63
64 //! Print message to log.
65 void print(const char* module, LogLevel level, const char* format, ...)
66 ROC_ATTR_PRINTF(4, 5);
67
68 //! Get current maximum log level.
70
71 //! Set maximum log level.
72 //!
73 //! @remarks
74 //! Messages with higher log level will be dropped.
75 //!
76 //! @note
77 //! Default log level is LogError.
79
80 //! Set log handler.
81 //!
82 //! @remarks
83 //! If @p handler is not NULL, log messages will be passed to @p handler.
84 //! Otherwise, they're printed to stderr.Default log handler is NULL.
85 void set_handler(LogHandler handler);
86
87 //! Set colors mode.
88 //!
89 //! @note
90 //! Default colors mode is ColorsAuto.
92
93private:
94 friend class Singleton<Logger>;
95
96 Logger();
97
98 Mutex mutex_;
99
100 LogLevel level_;
101 LogHandler handler_;
102 ColorsMode colors_;
103};
104
105} // namespace core
106} // namespace roc
107
108#endif // ROC_CORE_LOG_H_
GCC attributes.
#define ROC_ATTR_PRINTF(n_fmt_arg, n_var_arg)
Function gets printf-like arguments.
Definition: attributes.h:28
Logger.
Definition: log.h:57
void set_colors(ColorsMode mode)
Set colors mode.
void print(const char *module, LogLevel level, const char *format,...) ROC_ATTR_PRINTF(4
Print message to log.
void LogLevel level()
Get current maximum log level.
void set_handler(LogHandler handler)
Set log handler.
static Logger & instance()
Get logger instance.
Definition: log.h:60
void set_level(LogLevel)
Set maximum log level.
Mutex.
Definition: mutex.h:27
Base class for non-copyable objects.
Definition: noncopyable.h:23
static T & instance()
Get singleton instance.
Definition: singleton.h:29
Mutex.
const ColorsMode DefaultColorsMode
Default colors mode.
Definition: log.h:51
const LogLevel DefaultLogLevel
Default log level.
Definition: log.h:42
void(* LogHandler)(LogLevel level, const char *module, const char *message)
Log handler.
Definition: log.h:54
ColorsMode
Colors mode.
Definition: log.h:45
@ ColorsDisabled
Do not use colored logs.
Definition: log.h:46
@ ColorsEnabled
Use colored logs.
Definition: log.h:47
Root namespace.
LogLevel
Log level.
Definition: log.h:31
@ LogNone
Disable all messages.
Definition: log.h:32
@ LogDebug
Debug message.
Definition: log.h:35
@ LogError
Error message.
Definition: log.h:33
@ LogTrace
Debug message (extra verbosity).
Definition: log.h:36
@ LogInfo
Informational message.
Definition: log.h:34
Non-copyable object.
Singleton.